package com.usdt.system.mapper;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usdt.common.core.domain.entity.SysFile;
import com.usdt.common.core.domain.model.QueryFileInfoVo;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author LG
 * @since 2023-01-03
 */
public interface SysFileMapper extends BaseMapper<SysFile> {

	default List<SysFile> queryByKeys(List<String> keys) {
        return selectList(new QueryWrapper<SysFile>().in("file_key", keys));
    }


    @Select("select file_name from sys_file where file_key=#{fileKey}")
    String getFileName(String fileKey);

    default SysFile get(String key) {
        List<SysFile> list = selectList(new QueryWrapper<SysFile>().eq("file_key", key));
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    default int del(String key) {
        return delete(new QueryWrapper<SysFile>().eq("file_key", key));
    }

    default List<SysFile> query(QueryFileInfoVo in) {

        QueryWrapper<SysFile> qw = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(in.getKeyword())) {
            qw.and(
                    q -> q.like("file_name", in.getKeyword())
                            .or().like("file_key", in.getKeyword())
                            .or().like("business_code", in.getKeyword())
                            .or().like("app_name", in.getKeyword())
                            .or().like("uploader_name", in.getKeyword())
            );
        }

        if (in.getStartTime() != null) {
            //>=
            qw.ge("create_date", in.getStartTime());
        }

        if (in.getEndTime() != null) {
            qw.le("create_date", in.getEndTime());
        }
        if (StringUtils.isNotEmpty(in.getBusinessCode())) {
            qw.eq("business_code", in.getBusinessCode());
        }
        qw.orderByDesc("create_date");
        IPage<SysFile> page = new Page<>(in.getPageNum(), in.getPageSize());
        IPage<SysFile> page2 = selectPage(page, qw);
        page.setTotal((int) page2.getTotal());
        return page2.getRecords();
    }
}
